import 'package:flutter/foundation.dart';

import 'screen_adapt.dart';

class YYYDeviceInfo {
  static const Size defaultSize = Size(375, 667);
  static late YYYDeviceInfo _instance;

  /// UI设计中手机尺寸
  late Size uiSize;

  /// 屏幕像素
  late double _devicePixelRatio;

  /// 屏幕宽度
  late double _screenWidth;

  /// 屏幕高度
  late double _screenHeight;

  /// 状态栏高度
  late double _statusBarHeight;

  /// 底部安全间距高度
  late double _bottomBarHeight;

  /// 文字缩放比例
  late double _textScaleFactor;

  YYYDeviceInfo._();
  factory YYYDeviceInfo() {
    return _instance;
  }

  /// 初始化
  static void initialize({Size designSize = defaultSize}) {
    _instance = YYYDeviceInfo._()..uiSize = designSize;

    /// 物理宽度
    double physicalWidth = window.physicalSize.width;
    double physicalHeight = window.physicalSize.height;

    /// 像素
    _instance._devicePixelRatio = window.devicePixelRatio;

    /// 屏幕宽高
    _instance._screenWidth = physicalWidth / window.devicePixelRatio;
    _instance._screenHeight = physicalHeight / window.devicePixelRatio;

    /// 状态栏、底部栏高度
    _instance._statusBarHeight = window.padding.top / window.devicePixelRatio;
    _instance._bottomBarHeight =
        window.padding.bottom / window.devicePixelRatio;

    _instance._textScaleFactor = window.textScaleFactor;

    /// 物理高度
    if (kDebugMode) {
      print('physicalWidth = $physicalWidth, physicalHeight = $physicalHeight');
      print('devicePixelRatio = ${_instance.devicePixelRatio}');
      print('screenWidth = ${_instance.screenWidth}');
      print('screenHeight = ${_instance.screenHeight}');
      print('statusBarHeight = ${_instance.statusBarHeight}');
      print('bottomBarHeight = ${_instance.bottomBarHeight}');
      print('window.padding.top = ${window.padding.top}');
      print('window.padding.bottom = ${window.padding.bottom}');
      print('textScaleFactor = ${_instance.textScaleFactor}');
    }
  }

  double get devicePixelRatio => _devicePixelRatio;

  double get screenWidth => _screenWidth;

  double get screenHeight => _screenHeight;

  double get statusBarHeight => _statusBarHeight;

  double get bottomBarHeight => _bottomBarHeight;

  double get textScaleFactor => _textScaleFactor;

  /// 实际尺寸与UI设计稿的比例
  double get scaleWidth => _screenWidth / uiSize.width;

  /// 实际尺寸与UI设计稿的比例
  double get scaleHeight => _screenHeight / uiSize.height;

  /// 文本缩放
  double get scaleText => min(scaleWidth, scaleHeight);

  double setWidth(num width) => width * scaleWidth;

  double setHeight(num height) => height * scaleHeight;

  /// 字体缩放
  double setSp(num fontSize) => fontSize * scaleText;
}
